package gw.com.android.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.AppMain;
import gw.com.android.app.GTConfig;
import www.com.library.dialog.ToastPopWindow;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.CommonUtils;
import www.com.library.util.DeviceUtil;

/**
 * Created by jay on 2017/7/3.
 */

public class BindCustomerDialog extends BaseDialog{

    DataItemResult mResult;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private AccountCheckAdapter mAccountCheckAdapter;
    private RecyclerView popListView;
    private BindEnterPasswordDialog.BtnConfirmListener callback;// 点击事件的回调函数
    private DataItemDetail checkedItem;
    private String mContent = "";
    private TextView mView;
    private int bindAccountType;
    private static boolean isPhoneLogin;
    public BindCustomerDialog(Context context,String content,int accountType, DataItemResult mResult,BindEnterPasswordDialog.BtnConfirmListener callback) {
        super(context);
        this.mResult = mResult;
        this.callback = callback;
        this.mContent = content;
        this.bindAccountType = accountType;
    }

    /**
     * 显示弹层
     * @param act
     */
    public static void showBindCustomerDialog(Activity act,boolean isPhone,String content ,int accountType,DataItemResult mResult,BindEnterPasswordDialog.BtnConfirmListener callback){
        isPhoneLogin = isPhone;
        mInstance = new BindCustomerDialog(act,content,accountType,mResult,callback);
        mInstance.setCanceledOnTouchOutside(false);
        mInstance.show();
    }

    @Override
    public void initParam() {
        mCustomViewResId = R.layout.dialog_bind_customer_content;
        if(isPhoneLogin){
            mBtnPosText = AppMain.getAppString(R.string.btn_confirm);
            mBtnNegText = AppMain.getAppString(R.string.btn_cancel);
        }else{
            mBtnPosText = AppMain.getAppString(R.string.btn_confirm);
            mBtnNegText = AppMain.getAppString(R.string.bind_cancel);
        }

        int padding = (int) (15 * DeviceUtil.instance().getScreenDensity(mOwnerAct));
        setPadding(padding, 0, padding, 0);
    }

    @Override
    public void inflaterCustomView(View mCustomView) {
        popListView = (RecyclerView)mCustomView.findViewById(R.id.account_list);
        mView = (TextView) mCustomView.findViewById(R.id.dialog_title_text);
        mAccountCheckAdapter = new AccountCheckAdapter(mOwnerAct);
        // 线性布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(mOwnerAct);
        // 设置布局管理器
        popListView.setLayoutManager(layoutManager);
//        popListView.addItemDecoration(new RecycleViewDivider(mOwnerAct, LinearLayoutManager.VERTICAL));
        popListView.setAdapter(mAccountCheckAdapter);
        if(mContent.length() > 0){
            mView.setText(mContent);
        }

        if(isPhoneLogin){
            popListView.setVisibility(View.GONE);
        }else{
            popListView.setVisibility(View.VISIBLE);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 确认绑定
            case R.id.action_btn_pos:
//               callback.onBtnClick(checkedItem);
                if(isPhoneLogin){
                    dismiss();
                }else{
                    String dialogContent;
                    if(null == checkedItem){
                        new ToastPopWindow(mOwnerAct, AppMain.getAppString(R.string.demo_account_null)).show();
                        return;
                    }else{
                        if(bindAccountType == GTConfig.ACC_TYPE_DEMO){
                            dialogContent = AppMain.getAppString(R.string.demo_bind_enter_password_account,checkedItem.getString("customerNumber"));
                        }else{
                            dialogContent = AppMain.getAppString(R.string.real_bind_enter_password_account,checkedItem.getString("customerNumber"));
                        }
                        BindEnterPasswordDialog.showBindEnterPasswordDialog(mOwnerAct,isPhoneLogin,dialogContent,checkedItem, callback);
                        dismiss();
                    }
                }
                break;
            // 取消
            case R.id.action_btn_neg:
                dismiss();
                break;
        }
    }

   public  class AccountCheckAdapter extends RecyclerView.Adapter{
        private LayoutInflater mInflater;

        public AccountCheckAdapter(Activity act){
            mInflater = LayoutInflater.from(act);
        }

       public DataItemDetail getItem(int position) {
           if (position < 0 || position >= mResult.getDataCount())
               return null;
           return mResult.getItem(position);
       }

       @Override
       public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
           View itemView = mInflater.inflate(R.layout.list_item_bind_account, parent,false);
           return new ItemView(itemView);
       }

       @Override
       public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
           ItemView viewHold = (ItemView) holder;
           if (viewHold.mView != null) {
               DataItemDetail mItem = getItem(position);
               viewHold.mView.setText(mItem.getString("customerNumber"));
               viewHold.mView.setTag(position);
               viewHold.mView.setChecked(mSelectedPositions.get(position, false));
               if(mSelectedPositions.get(position)){
                   viewHold.mView.setChecked(true);
                   viewHold.mView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.a_smalledit_check, 0, 0, 0);
               }else{
                   viewHold.mView.setChecked(false);
                   viewHold.mView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.a_smalledit_uncheck, 0, 0, 0);
               }
           }
       }

       @Override
       public int getItemCount() {
           if(null != mResult){
               return mResult.getDataCount();
           }
           return 0;
       }

       class ItemView extends RecyclerView.ViewHolder{

           @BindView(R.id.rb_bind)
           RadioButton mView;

           public ItemView(View itemView) {
               super(itemView);
               ButterKnife.bind(this, itemView);
           }

           @OnClick(R.id.bind_item_layout)
           void onClick() {
               if(CommonUtils.isFastDoubleClick()){
                   return;
               }
               int position = (int) mView.getTag();
               checkedItem = getItem(position);
               mView.setChecked(true);
               mSelectedPositions.clear();
               mSelectedPositions.put(position, mView.isChecked());
               mAccountCheckAdapter.notifyDataSetChanged();
//               mView.setChecked(true);
//               mView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.a_smalledit_check, 0, 0, 0);
           }
       }

   }

}
